package com.atguigu.exam.service.impl;

import com.atguigu.exam.common.GlobalExceptionHandler;
import com.atguigu.exam.entity.Banner;
import com.atguigu.exam.mapper.BannerMapper;
import com.atguigu.exam.service.BannerService;

import com.atguigu.exam.service.FileUploadService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.minio.errors.*;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Queue;

/**
 * 轮播图服务实现类
 */
@Service
@Slf4j
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements BannerService {

    @Autowired
    private BannerMapper bannerMapper;

    @Autowired
    private FileUploadService fileUploadService;

    /**
     * 获取所有轮播图
     * @return 轮播图列表
     */
    @Override
    public List<Banner> getAllBanners() {
        LambdaQueryWrapper<Banner> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Banner::getIsDeleted, 0).orderByAsc(Banner::getSortOrder);

        //逻辑删除字段和更新时间字段不返回
        List<Banner> banners = bannerMapper.selectList(queryWrapper);

        log.info("查询所有轮播图成功: {}", banners);

        return banners;
    }

    /**
     * 获取所有启用的轮播图
     * @return 轮播图列表
     */
    @Override
    public List<Banner> getActiveBanners() {
        LambdaQueryWrapper<Banner> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Banner::getIsDeleted, 0)
                .eq(Banner::getIsActive, true)
                .orderByAsc(Banner::getSortOrder);

        List<Banner> banners = bannerMapper.selectList(queryWrapper);
        log.info("查询所有启用的轮播图成功: {}", banners);

        return banners;
    }

    /**
     * 上传轮播图图片
     * @param file 图片文件
     * @return 图片URL
     */
    @Override
    public String uploadBannerImage(MultipartFile file) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {

        //1.参数校验
        if (file == null || file.isEmpty()) {
            throw new RuntimeException("上传文件不能为空");
        }

        //2.判断是否为图片
        String contentType = file.getContentType();
        if (!contentType.startsWith("image")) {
            throw new RuntimeException("上传文件必须是图片");
        }

        //3.文件大小限制
        long size = file.getSize();
        if (size > 1024 * 1024 * 5) {
            throw new RuntimeException("上传文件大小不能超过5M");
        }

        //4.上传图片
        String imageUrl = fileUploadService.uploadFile(file, "banner");

        log.info("上传轮播图图片成功: {}", imageUrl);

        return imageUrl;
    }

    /**
     * 添加轮播图
     * @param banner 轮播图信息
     */
    @Override
    public void addBanner(Banner banner) {

        if (banner.getIsActive()==null){
            banner.setIsActive(true);
        }

        if (banner.getSortOrder()==null){
            banner.setSortOrder(0);
        }

        int insert = bannerMapper.insert(banner);
        if (insert <= 0) {
            throw new RuntimeException("添加轮播图失败");
        }
        log.info("添加轮播图成功: {}", banner);
    }

    /**
     * 修改轮播图
     * @param banner 轮播图信息
     */
    @Override
    public void updateBanner(Banner banner) {
        this.updateById(banner);
        log.info("修改轮播图成功: {}", banner);
    }
}